package com.enba.boot.log.config;

import com.enba.boot.core.base.ShowBootInfo;
import com.enba.boot.log.LogHelper;
import com.enba.boot.log.properties.LogProperties;
import com.enba.boot.log.strategy.MongoStrategy;
import com.enba.boot.log.strategy.MySqlStrategy;
import javax.annotation.PostConstruct;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(LogProperties.class)
public class LogAutoConfiguration implements ShowBootInfo {

  private final LogProperties logProperties;

  public LogAutoConfiguration(LogProperties logProperties) {
    this.logProperties = logProperties;
  }

  @Bean
  @ConditionalOnProperty(name = "log.target", havingValue = "mongodb")
  @ConditionalOnClass(name = "com.mongodb.client.MongoClients")
  @ConditionalOnMissingBean(LogHelper.class)
  public LogHelper mongoDBLoggingService() {
    return new MongoStrategy(logProperties);
  }

  @Bean
  @ConditionalOnProperty(name = "log.target", havingValue = "mysql")
  @ConditionalOnClass(name = "org.springframework.jdbc.core.JdbcTemplate")
  @ConditionalOnMissingBean(LogHelper.class)
  public LogHelper mysqlLoggingService() {
    return new MySqlStrategy(logProperties);
  }

  @PostConstruct
  @Override
  public void showBootInfo() {
    ShowBootInfo.moduleMap.put("enba-boot-starter-log", "enba-boot-starter-log模块集成成功");
  }
}
